//
//  UIColor.swift
//  
//
//  Created by tangbl93 on 2021/1/6.
//

import UIKit
import UIColor_Hex_Swift

public extension UIColor {
    
    /// The rgba string representation of color
    var yx_rgb:String { return hexString(false) }
    var yx_rgba:String { return hexString(true) }
    
    /// The six-digit hexadecimal representation of color with alpha of the form #RRGGBBAA
    convenience init(_ rgb: UInt32) {
        self.init(hex6: rgb)
    }
    
    ///  The rgba string representation of color with alpha of the form #RRGGBBAA/#RRGGBB
    convenience init(_ rgba: String) {
        self.init(rgba, defaultColor: UIColor.clear)
    }
}

public extension UIColor {
    
    /// rendering color to image
    func yx_rending(size: CGSize = CGSize.yx_one) -> UIImage? {
        let rect = CGRect(origin: .zero, size: size)
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
        self.setFill()
        UIRectFill(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        guard let cgImage = image?.cgImage else { return nil }
        return UIImage(cgImage: cgImage)
    }
}
